﻿; (function (window, undefined, $) {
    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl,
            colModel: [
                    { display: '编号', name: 'Id', width: "5%", sortable: true, hide: false, align: 'left', iskey: true },
                    { display: '角色名', name: 'RoleName', width: "10%", sortable: false, align: 'left' },
                    { display: '删除标志', name: 'DelFlag', width: "10%", sortable: false, align: 'left', process: fromateDelete },
                    { display: '提交时间', name: 'SubTime', width: "10%", sortable: false, align: 'left' },
                    { display: '备注', name: 'Remark', width: "10%", sortable: false, align: 'left' },
                    { display: '改进时间', name: 'ModifiedOn', width: "10%", sortable: false, align: 'left' },
                    { display: '排序', name: 'Sort', width: "10%", sortable: false, align: 'left' },
                    { display: '操作', name: 'Id', width: "10%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "Id",
            sortorder: "DESC",
            title: false,
            rp: 14,
            usepager: true,
            showcheckbox: false
        };

        var xjgrid = new xjGrid("gridlist", gridopt);

        function processOp(value, cell) {
            var ops = [];
            ops.push("&nbsp;<a title='编辑' class='btn btn-info btn-xs edit' href='javascript:;'  onclick=\"util.Edit('", value, "')\"><i class='fa fa-edit' ></i>编辑</a>");
            ops.push("&nbsp;&nbsp;<a title='删除' class='btn btn-danger btn-xs delete' href='javascript:;'  onclick=\"util.Delete('", value, "','", cell[1], "')\"><i class='fa fa-trash-o' ></i>删除</a>");
            return ops.join("");
        }

        $("#formQuery").submit(function () {
            xjgrid.Query(this);
            return false;
        });

        function fromateDelete(value, cell) {
            if (value == '0') {
                return '正常使用';
            } else if (value == '1') {
                return '已删除';
            }
        }

        $("#btnSearch").click(function () {
            $("#formQuery").submit();
        });

        $("#btnAdd").click(function (e) {
            var url = options.editUrl;
            location.href = url;
        });

        $("#btnReset").click(function () {
            $("#Name").val(null);
            $('#PublishTime').val(null);
            $("#formQuery").submit();
        });

        util.Edit = function (Id) {
            var re = /^.*\?.+=.+$/;
            var url = options.editUrl;
            if (re.test(options.detailUrl)) {
                url += "&Id=" + Id;
            } else {
                url += "?Id=" + Id;
            }
            location.href = url;
        };

        util.Delete = function (Id, name) {
            bootbox.dialog({
                title: "提示",
                message: "您确定要执行删除操作吗？",
                className: "modal-warning",
                buttons: {
                    success: {
                        label: "确定",
                        className: "btn btn-primary",
                        callback: function () {
                            $.post(options.deleteUrl + "/" + Id, { Id: Id },
                                  function (res) {
                                      if (res.Status > 0) {
                                          bootbox.dialog({
                                              title: "提示",
                                              message: "操作成功！",
                                              className: "modal-success",
                                              buttons: {
                                                  ok: {
                                                      label: "确定"
                                                  }
                                              }
                                          });
                                          xjgrid.Reload();
                                      }
                                      else {
                                          bootbox.dialog({
                                              title: "提示",
                                              message: res.Message,
                                              className: "modal-danger",
                                              buttons: {
                                                  ok: {
                                                      label: "确定"
                                                  }
                                              }
                                          });
                                      }
                                  },
                                  "json"
                            );
                        }
                    },
                    cancel: {
                        label: "取消",
                        className: "btn btn-warning"
                    }
                }
            });
        };
    });

})(window, undefined, jQuery);